package emediashared.ui;

import java.io.File;
import java.net.InetAddress;
import java.net.UnknownHostException;

import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.IDialogSettings;
import org.eclipse.jface.layout.GridDataFactory;
import org.eclipse.jface.layout.GridLayoutFactory;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.DirectoryDialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;

import emediashared.SharedActivator;
import emediashared.model.SharedConstants;


public class PreferencesDialog extends Dialog {
	private static final String DEFAULT_LOCAL_PATH = "C:\\ESharedPlugin\\local";
	private static final String DEFAULT_REMOTE_PATH = "C:\\ESharedPlugin\\remote";

	private static final String LOCAL_PATH = "emediashared.ui.ESharedPreferencesDialog.localPath";
	private static final String REMOTE_PATH = "emediashared.ui.ESharedPreferencesDialog.remotePath";
	private static final String USER_NAME = "emediashared.ui.ESharedPreferencesDialog.userName";

	private String local;
	private String remote;
	private String userName;
	private Text localText;
	private Text remoteText;
	private Text userNameText;
	private boolean notify;
	private Button notifyButton;

	public PreferencesDialog() {
		super(Display.getDefault().getActiveShell());
		this.local = getLocalPath();
		this.remote = getRemotePath();
		this.userName = getUserName();
		this.notify = SharedActivator.getDefault().getPreferenceStore().getBoolean(SharedConstants.PREFERENCE_NOTIFY);
		setShellStyle(getShellStyle() | SWT.RESIZE);
	}

	@Override
	protected Control createDialogArea(Composite parent) {
		getShell().setText("Preferences");
		Composite container = new Composite(parent, SWT.NONE);
		GridDataFactory.fillDefaults().grab(true, false).span(400, SWT.DEFAULT).applyTo(container);
		GridLayoutFactory.fillDefaults().numColumns(3).margins(5, 5).applyTo(container);
		
		Label label = new Label(container, SWT.NONE);
		label.setText("User: ");
		GridDataFactory.swtDefaults().applyTo(label);
		userNameText = new Text(container, SWT.BORDER);
		userNameText.setText(userName);
		GridDataFactory.fillDefaults().grab(true, false).span(2, 1).applyTo(userNameText);
		
		notifyButton = new Button(container, SWT.CHECK);
		notifyButton.setText("Enable Notifications");
		notifyButton.setSelection(notify);
		notifyButton.setVisible(false);
		GridDataFactory.swtDefaults().span(3, 1).exclude(true).applyTo(notifyButton);
		
		localText = createRow(container, "Local: ", local);
		remoteText = createRow(container, "Remote: ", remote);

		return container;
	}

	private Text createRow(Composite container, String displayName, String initValue) {
		Label label = new Label(container, SWT.NONE);
		label.setText(displayName);
		GridDataFactory.swtDefaults().applyTo(label);
		final Text text = new Text(container, SWT.BORDER);
		text.setText(initValue);
		GridDataFactory.fillDefaults().grab(true, false).applyTo(text);
		
		Button browseButton = new Button(container, SWT.PUSH);
		browseButton.setText("...");
		browseButton.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				DirectoryDialog dialog = new DirectoryDialog(Display.getDefault().getActiveShell());
				dialog.setFilterPath(text.getText());
				String path = dialog.open();
				if (path != null) {
					text.setText(path);
				}
			}
		});
		GridDataFactory.swtDefaults().applyTo(browseButton);
		
		return text;
	}

	@Override
	protected void okPressed() {
		local = localText.getText();
		remote = remoteText.getText();
		userName = userNameText.getText();
		notify = notifyButton.getSelection();
		SharedActivator.getDefault().getPreferenceStore().setValue(SharedConstants.PREFERENCE_NOTIFY, notify);
		IDialogSettings dialogSettings = SharedActivator.getDefault().getDialogSettings();
		dialogSettings.put(LOCAL_PATH, local);
		dialogSettings.put(REMOTE_PATH, remote);
		dialogSettings.put(USER_NAME, userName);
		super.okPressed();
	}

	
	
	public static String getUserName() {
		String userName = SharedActivator.getDefault().getDialogSettings().get(USER_NAME);
		if (userName == null) {
			try {
				userName = InetAddress.getLocalHost().getHostName() + "-" + new File(ResourcesPlugin.getWorkspace().getRoot().getLocationURI()).getName();
			} catch (UnknownHostException e) {
				SharedActivator.showAndLogError(e);
			}

		}
		return userName;
	}
	
	public static String getLocalPath() {
		String path = SharedActivator.getDefault().getDialogSettings().get(LOCAL_PATH);
		if (path == null) {
			path = DEFAULT_LOCAL_PATH;
		}
		return path;
	}
	
	public static String getDownloadPath() {
		String path = getLocalPath();
		String[] split = path.split(SharedConstants.PATH_DELIMITTER);
		return split[0];
	}

	public static String getRemotePath() {
		String path = SharedActivator.getDefault().getDialogSettings().get(REMOTE_PATH);
		if (path == null) {
			path = DEFAULT_REMOTE_PATH;
		}
		return path;

	}
	
	public static File getUsersFile() {
		String remotePath = getRemotePath();
	    File file = new File(remotePath, SharedConstants.USER_PROPERTIES_FILE);
		return file;
	}
	
	public static File getRepoFile() {
		String remotePath = getRemotePath();
	    File file = new File(remotePath, SharedConstants.REPO_PROPERTIES_FILE);
		return file;

	}
}
